﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace FileViewer
{
    class FileManager
    {
        /// <summary>
        /// Директория в которой находится менеджер
        /// </summary>
        public string NowDirectory { get; set; }

        /// <summary>
        /// история перемещений
        /// </summary>
        public Stack<string> History { get; private set; }

        private FileManager()
        {
        }

        public FileManager(string directory)
        {
            History = new Stack<string>();
            ScanDir(directory);

        }

        List<DirectoryInfo> _Directories;
        List<FileInfo> _Files;


        void ScanDir(string directory)
        {
            DirectoryInfo dinfo = new DirectoryInfo(directory);

            _Files = new List<FileInfo>(dinfo.GetFiles());
            _Directories = new List<DirectoryInfo>(dinfo.GetDirectories());

            NowDirectory = directory;
            History.Push(NowDirectory);
        }


        /// <summary>
        /// открывает файл или директорию папки в которой находится
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool Open(string Name)
        {
            bool d = Directory.Exists(NowDirectory + "\\" + Name);
            bool f = File.Exists(NowDirectory + "\\" + Name);

            if (d)
            {
                ScanDir(NowDirectory + "\\" + Name);
                return true;
            }

            if (f)
            {

                System.Diagnostics.Process.Start(NowDirectory + "\\" + Name);
                return true;
            }  

            return false;
        }
        
        /// <summary>
        /// выдает имена файлов и папок в директории
        /// </summary>
        public List<string> Files
        {
            get
            {
                List<string> A = new List<string>();

                for (int i = 0; i < _Files.Count; i++)                
                    A.Add(_Files[i].Name);

                for (int i = 0; i < _Directories.Count; i++)
                    A.Add(_Directories[i].Name);

                return A;
            }
        }

        public bool GoBack()
        {
            if (History.Count == 0)
                return false;

            History.Pop();
            ScanDir(History.Pop());

            return true;
        }
    }
}
